package org.shoukaiseki.common.utils

import com.github.promeg.pinyinhelper.Pinyin
import java.lang.RuntimeException
import java.lang.StringBuilder

object TinyPinyinUtils {
    /**
     * 任何一个是中文
     * @param str
     * @return          true:存在中文
     */
    @JvmStatic
    fun anyChinese(str: String?): Boolean {
        if (str == null) {
            return false
        }
        var b = false
        for (c in str.toCharArray()) {
            if (Pinyin.isChinese(c)) {
                b = true
            }
        }
        return b
    }

    /**
     * 全部是中文
     * @param str
     * @return  true:全部是中文
     */
    @JvmStatic
    fun allChinese(str: String?): Boolean {
        if (str == null) {
            return false
        }
        var b = true
        for (c in str.toCharArray()) {
            if (Pinyin.isChinese(c)) {
                b = false
            }
        }
        return b
    }

    /**
     * 获取中文拼音首字母
     * @param str
     * @param type      0:全部小写; 1:全部大写
     * @return
     */
    @JvmStatic
    @JvmOverloads
    fun getChinesePinyinInitials(str: String?, type: Int=1): String {
        if (str == null) {
            return ""
        }
        val sb = StringBuilder()
        for (c in str.toCharArray()) {
            if (Pinyin.isChinese(c)) {
                var tmp = Pinyin.toPinyin(c).substring(0, 1)
                if (type == 1) {
                    tmp = tmp.toUpperCase()
                }else if(type ==0){
                    tmp = tmp.toLowerCase()
                }else{
                    throw RuntimeException("无效的type=${type}")
                }
                sb.append(tmp)
            }
        }
        return sb.toString()
    }
}